package com.atguigu.core.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;
import java.util.ArrayList;
import java.util.List;

/**
 * knife4j配置信息
 *
 * @author atguigu
 * @created 2022-10-25-23:45.
 */
@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfig {
    @Value("${swaggerConfig.enable}")
    private Boolean enable;
    @Value("${server.servlet.context-path}")
    private String contextPath;

    private static String COMPANY_HOME = "atguigu";

    @Bean
    public Docket adminApiConfig() {
        List<Parameter> pars = getPars("token", "用户token");
        String moduleName = "admin";
        return getDocketByName(pars, moduleName);
    }

    private List<Parameter> getPars(String token, String s) {
        List<Parameter> pars = new ArrayList<>();
        ParameterBuilder tokenPar = new ParameterBuilder();
        tokenPar.name(token)
                .description(s)
                .defaultValue("")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .build();
        pars.add(tokenPar.build());
        //添加head参数end
        return pars;
    }

    private Docket getDocketByName(List<Parameter> pars, String moduleName) {
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(enable)
                .groupName(moduleName + "Api")
                .apiInfo(apiInfo(moduleName))
                .select()
                //只显示moduleName路径下的页面
                .apis(RequestHandlerSelectors.basePackage("com"))
                //只有是moduleName开头的才显示
                .build()
                .globalOperationParameters(pars)
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts());
    }

    private ApiInfo apiInfo(String moduleName) {


        return new ApiInfoBuilder()
                .title(moduleName + "系统-API文档")
                .description("本文档描述了" + moduleName + "系统微服务接口定义")
                .version("1.0")
                .contact(new Contact(COMPANY_HOME, "http://" + COMPANY_HOME + ".com", COMPANY_HOME + "@qq.com"))
                .build();
    }

    private List<ApiKey> securitySchemes() {
        List<ApiKey> apiKeyList = new ArrayList<>();
        apiKeyList.add(new ApiKey("token", "token", "header"));
        // apiKeyList.add(new ApiKey("access_token", "access_token", "query"));
        return apiKeyList;
    }

    private List<SecurityContext> securityContexts() {
        List<SecurityContext> securityContexts = new ArrayList<>();
        securityContexts.add(SecurityContext.builder()
                //
                .securityReferences(defaultAuth())
                // 所有包含"auth"的接口不需要使用securitySchemes
                .forPaths(PathSelectors.regex("^(?!auth).*$")).build());
        return securityContexts;
    }

    private List<SecurityReference> defaultAuth() {
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = new AuthorizationScope("global", "accessEverything");
        List<SecurityReference> securityReferences = new ArrayList<>();
        securityReferences.add(new SecurityReference("Authorization", authorizationScopes));
        return securityReferences;
    }
}
